﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace Jy123.Web.Friend
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class AlterFriendtoNewClass : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //int userID = 9;//暂时写死，当前用户ID
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            int userID = int.Parse(strUserIdFromCookie);

            //---------------------------------------------------------------------------
            string type = context.Request["Type"];
            string strFriendUserID = context.Request["FriendUserID"];
            string strFriendID = context.Request.Form["hidFrdID"];
            string strFclass = context.Request.Form["selClass"];
            
            if (type == "submit")//提交修改
            {
                int alterSuccess = AlterFrdToNewClass(Convert.ToInt32(strFclass), userID, Convert.ToInt32(strFriendID));
                context.Response.Redirect("/Friend/ListMain.aspx");
            }
            else
            {
                //写出界面
                string strFrdNewClassList = context.Cache["user_friend_tonewclass"].ToString();
                strFrdNewClassList = strFrdNewClassList.Replace("$FriendClassOption$",FriendClassSelect(context,userID));
                strFrdNewClassList = strFrdNewClassList.Replace("$strFriendUserID$",strFriendUserID);
                context.Response.ContentType = "text/plain";
                context.Response.Write(strFrdNewClassList);
            }
            
        }
        /// <summary>
        /// 好友分类选择下拉表
        /// </summary>
        /// <param name="userID">当前用户的ID</param>
        /// <returns>字符串</returns>
        private string FriendClassSelect(HttpContext context,int userID)
        {
            //好友分类选择下拉表
            StringBuilder sbFriendSelect = new StringBuilder();
            string htmFriendSelect = context.Cache["user_friendclass_select_option"].ToString();
            List<Jy123.Model.User_FriendClass> listFriendClass = new Jy123.BLL.User_FriendClass().GetModelList("FUser='" + userID + "'");
            foreach (Jy123.Model.User_FriendClass frdC in listFriendClass)
            {
                List<int?> count;
                string strFrdSel = htmFriendSelect;
                strFrdSel = strFrdSel.Replace("$CID$", frdC.ID.ToString());
                strFrdSel = strFrdSel.Replace("$CName$", frdC.CName);
                DataTable dtFriendList = new BLL.User.Friend().GetFriendList(1, int.MaxValue, userID, frdC.ID, out count).Tables[0];
                //int? frdCount = count[0];
                int? frdCount = count[0];
                strFrdSel = strFrdSel.Replace("$FrdCount$", frdCount.ToString());
                sbFriendSelect.Append(strFrdSel);
            }
            return sbFriendSelect.ToString();
        }
        /// <summary>
        /// 修改指定好友的分类
        /// </summary>
        /// <param name="newClassID">新选中的分类ID</param>
        /// <param name="userID">当前用户ID</param>
        /// <param name="frdID">好友的ID</param>
        /// <returns>0为修改失败、大于0为修改成功。</returns>
        private int AlterFrdToNewClass(int newClassID,int userID,int frdID)
        {
            string strSql = "update User_Friend set ffriendclass=@NewClassID where fuser=@UserID and ffriend=@FFriendID";
            SqlParameter[] parameters = { 
                                        new SqlParameter("@NewClassID", SqlDbType.Int),
                                        new SqlParameter("@UserID", SqlDbType.Int),
                                        new SqlParameter("@FFriendID", SqlDbType.Int)};
            parameters[0].Value = newClassID;
            parameters[1].Value = userID;
            parameters[2].Value = frdID;
            int effectRows = 0;
            effectRows = DBUtility.DbHelperSQL.ExecuteSql(strSql, parameters);
            return effectRows;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
